#!/bin/bash
# USAGE:
#	addrefpha2crop
#
# DESCRIPTION:
#  ADDREFPHA2CROP is an internal ADORE command. 
#  It is used in creating interferograms coregistered to a different scene, 
#  as needed in Small Baselines (SBAS) interferometry. The command adds the  
#  reference phase dumped by subtrrefpha (srp_dumprefpha=on) to the result
#  of resampling step and modifies the slave result file accordingly. The 
#  original result file is saved with a .bck extension.
#
# INPUT:
#  There are no required input parameters for this command.
#  
# OUTPUT:
#   Modifies the result file by moving the resampling step to crop. 
#   Also generates an output file with ".addrefpha" extension.
#

function funResample2Tmpfile(){
  tmpFile=${1}
  [ ! -e "${rs_out_file}" ] && error "Resample file not found. Please run resample step" && return 
  #[ ! -e "${srp_out_refpha}" ] && error "Reference phase file not found. Please run subtrrefpha with dumprefpha=on" && return 
  if [ ! -e "${srp_out_refpha}" ] && [ ! -e "${crd_out_file}" ] ; then
    error "Reference phase file not found. Please run subtrrefpha with dumprefpha=on or comprefdem with crd_include_fe=on"
    return 
  fi
  
  if [ -e "${crd_out_file}" ]; then 
    bold "Adding reference phase and topographic phase to resampled slave..."
    local fileName resFile format width numlines
    call "(fileName resFile format width numlines)=dorisProcess2OutputFile comprefdem"
    ${ADOREFOLDER}/lib/python/real2cpx.py -w ${width} -f ${format} -m 1 -p ${crd_out_file} ${crd_out_file}.cpx
    [ $? -ne 0 ] && error "real2cpx failed." && return 1
    cpxmult ${rs_out_file} ${crd_out_file}.cpx ${rs_out_file}.addrefpha 1
    [ $? -ne 0 ] && error "cpxmult can not multiply." && return 1
  else
    bold "Adding reference phase to resampled slave..."
    cpxmult ${rs_out_file} ${srp_out_refpha} ${rs_out_file}.addrefpha 1
    [ $? -ne 0 ] && error "cpxmult can not multiply." && return 1
  fi    
  cp ${s_resfile} ${s_resfile}.bck

  # Below line copies the information from the requested step and copies it to a temporary file
  #sed -n "/_Start_resample/,/End_resample/p" ${s_resfile} | sed -n '/\*\*\*\*\*\*/,/\*\*\*\*\*\*/p'  > ${tmpFile}
  #sed -n "/_Start_resample/,/End_resample/p" ${s_resfile}  | sed -e 's/original_master/original_image/g' > ${tmpFile}
  sed -n "/_Start_resample/,/End_resample/p" ${s_resfile} > ${tmpFile}
  # Remove the extra output lines
  sed -i -e '/Data_output_file_/ d' -e '/Data_output_format_/ d' ${tmpFile}
  # Replace the values of the filename and format, < is the split character $@ has a special meaning. can't use @
  sed -i -e "s<^Data_output_file.*$<Data_output_file: ${rs_out_file}.addrefpha<g" ${tmpFile}
  local azoff rgoff val
  azoff=`readRes.sh ${i_resfile} coarse_orbits "Coarse_orbits_translation_lines"`
  rgoff=`readRes.sh ${i_resfile} coarse_orbits "Coarse_orbits_translation_pixels"`
  #update first line
  val=`readRes.sh ${tmpFile} resample "First_line (w.r.t. original_master)" `  
  #modifyRes.sh ${tmpFile} resample "First_line (w.r.t. original_image)" $(( ${val}+${azoff} )) > ${tmpFile} ### ModifyRes does not like file being changed when it is looping in it.
  sed -i "s/^First_line.*$/First_line (w.r.t. original_image):		$(( ${val}+${azoff} ))/g" ${tmpFile}
  #update last line
  val=`readRes.sh ${tmpFile} resample "Last_line (w.r.t. original_master)" `  
  sed -i "s/^Last_line.*$/Last_line (w.r.t. original_image):		$(( ${val}+${azoff} ))/g" ${tmpFile}
  #modifyRes.sh ${tmpFile} resample "Last_line (w.r.t. original_image)" $(( ${val}+${azoff} )) > ${tmpFile}
  #update first pixel 
  val=`readRes.sh ${tmpFile} resample "First_pixel (w.r.t. original_master)" `  
  sed -i "s/^First_pixel.*$/First_pixel (w.r.t. original_image):		$(( ${val}+${rgoff} ))/g" ${tmpFile}
  #modifyRes.sh ${tmpFile} resample "First_pixel (w.r.t. original_image)" $(( ${val}+${rgoff} )) > ${tmpFile}
  #update last pixel
  val=`readRes.sh ${tmpFile} resample "Last_pixel (w.r.t. original_master)" `  
  sed -i "s/^Last_pixel.*$/Last_pixel (w.r.t. original_image):		$(( ${val}+${rgoff} ))/g" ${tmpFile}
  #modifyRes.sh ${tmpFile} resample "Last_pixel (w.r.t. original_image)" $(( ${val}+${rgoff} )) > ${tmpFile}
  #Remove the resample step header
  sed -i -n '/\*\*\*\*\*\*/,/\*\*\*\*\*\*/p' ${tmpFile}
}

function funResample2Crop(){
  generateRandomString
  local tmpFile=${randomString}
 
  funResample2Tmpfile ${tmpFile}
  [ $? -ne 0 ] && return 1 
  
  undo only crop ${s_resfile}
  undo only resample ${s_resfile}

  # Re-write crop for resampling.
  sed -i -e '/^crop/s/0/1/g' ${s_resfile}
  echo "*******************************************************************" >> ${s_resfile}
  echo "*_Start_crop:			master step01" >> ${s_resfile}
  cat ${tmpFile} >> ${s_resfile}
  echo "* End_crop:_NORMAL" >> ${s_resfile}
  echo "*******************************************************************" >> ${s_resfile}

  rm -rf ${tmpFile}
}

function funResample2Ovs(){
  generateRandomString
  local tmpFile=${randomString}
 
  funResample2Tmpfile ${tmpFile}
  [ $? -ne 0 ] && return 1 
  
  undo only oversample ${s_resfile}
  undo only resample ${s_resfile}

  # Re-write crop for resampling.
  sed -i -e '/^oversample/s/0/1/g' ${s_resfile}
  echo "*******************************************************************" >> ${s_resfile}
  echo "*_Start_oversample:			master " >> ${s_resfile}
  cat ${tmpFile} >> ${s_resfile}
  echo "* End_oversample:_NORMAL" >> ${s_resfile}
  echo "*******************************************************************" >> ${s_resfile}

  rm -rf ${tmpFile}
}


###MAIN
if [ `readRes.sh ${s_resfile} process_control oversample` == "1" ]; then
  funResample2Ovs ${@}
elif [ `readRes.sh ${s_resfile} process_control crop` == "1" ]; then
  funResample2Crop ${@}
else
  error "addrefpha2s_crop: s_ovs or s_crop steps are not in s_resfile."
fi
